<template>
  <div class="com-container">
    <div class="com-chart" ref="RankBar"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      allData: null,//服务器返回的数据
      chartInstance: null,
      starValue:0,//起点值
      endValue:9,//终点值
      timeId:null
    };
  },
  
  //vue写方法的钩子
  methods: {
    //初始化echartsInstance对象
    initChart() {
      this.chartInit = this.$echarts.init(this.$refs.RankBar, this.theme);
      //初始化option
      const initOption = {
        title: {
          text: '|  地区销量',
          left: 20,
          top: 8
        },
        grid: {
          top: '20%',
          left: '5%',
          bottom: '5%',
          right: '5%',
          constainLable: true//从坐标开始定位
        },
        xAxis: {
          type: 'category',
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            type: 'bar'
          }
        ],
        tooltip: {
          show: true
        }

      }
      this.chartInit.setOption(initOption)
      this.chartInit.on('mouseover',()=>{
        clearInterval(this.timeId)
      })
      this.chartInit.on("mouseout",()=>{
        this.interval()
      })
    },
    //获取服务器数据
    async getData() {
      const { data: res } = await this.$http.get('rank');
      this.allData = res
      //从大到小大的排序
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.updataChart();
      this.interval()
    },
    //更新图表
    updataChart() {
      const nameArr = this.allData.map(item => {
        return item.name
      })
      const valueArr = this.allData.map(item => {
        return item.value
      })
      const colorArr = [
        ['#0BA82C', '#4FF778'],
        ['#2E72BF', '#23E5E5'],
        ['#5052EE', '#AB6EE5']
      ]
      //计算一共多少页
      //添加了数据的option
      const dataOption = {
        xAxis: {
          data: nameArr
        },
        dataZoom:{
          show:false,
          startValue:this.starValue,
          endValue:this.endValue
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: arg => {

                let targetColorArr = colorArr[0]
                if (arg.data >= 300) {
                  //console.log(arg.data)
                  targetColorArr = colorArr[0]
                } else if (arg.data >= 200) {
                  targetColorArr = colorArr[1]
                } else {
                  targetColorArr = colorArr[2]
                }
                //***** 重点  实现主张图的渐变 */
                return new this.$echarts.graphic.LinearGradient(0,
                  1, 0, 0, [
                  {
                    offset: 0,
                    color: targetColorArr[0]
                  },
                  {
                    offset: 1,
                    color: targetColorArr[1]
                  }
                ])
              }
            }
          }
        ]
      }
      this.chartInit.setOption(dataOption)
    },
    //动态适配电脑的分辨率
    resizeEcharts() {
      //动态获取容器的宽度
      const titleFontsize = this.$refs.RankBar.offsetWidth / 100 *3.6;

      const resizeOption = {
        title:{
          textStyle:{
            fontSize:titleFontsize
          }
        },
        series:[
          {
            barWidth:titleFontsize,
            itemStyle:{
              barBorderRadius:[0.5*titleFontsize,0.5*titleFontsize,0,0] //左上角，右上角 左下角 右下角 圆角边框的设置
            }
          }
        ]

      }
      this.chartInit.setOption(resizeOption)
      this.chartInit.resize()
    },
    //定时器
    interval(){
      if(this.timeId){
        clearInterval(this.timeId)
      }
      this.timeId=setInterval(()=>{
        this.starValue++
        this.endValue++
        //console.log(this.starValue+"****"+this.endValue)
        if(this.endValue>this.allData.length-1){
          this.starValue=0;
          this.endValue=9
        }
        this.updataChart();
      },1000)
    }
  },
  destroyed() {
    //销毁监听事件以防止内存泄漏
    window.removeEventListener('resize', this.resizeEcharts)
    clearInterval(this.timeId)
  },
  mounted() {
    this.initChart();
    this.getData(),
      //监听window窗口的大小变化
    window.addEventListener('resize', this.resizeEcharts)
    this.resizeEcharts()
  },
  computed: {
    ...mapState(['theme'])

  },
   watch: {
    theme() {
      this.chartInit.dispose()
      this.initChart()
      this.resizeEcharts()
      this.updataChart()
    }
  },
};
</script>

<style lang="less">
</style>